home *** CD-ROM | disk | FTP | other *** search
/ Info-Mac 4 / Info_Mac IV CD-ROM (Pacific HiTech Inc.)(August 1994).iso / Development / Source / GENetReleaseƒ / GEDemo / Cannon.c next >
Text File  |  1994-03-07  |  4KB  |  175 lines

  1. /*
  2.     Cannon.c
  3.     
  4.     Cannon scene for GEDemo
  5.     
  6.     Copyright 1993 by Al Evans. All rights reserved.
  7.     
  8.     11/3/93
  9.     
  10. */
  11.  
  12. #include "Cannon.h"
  13. #include "Motion.h"
  14.  
  15. static MotionParams    ballMotion;
  16.  
  17. Boolean LoadCannonScene(GEWorldPtr world)
  18. {
  19.     GrafElPtr        cannon, thisElement;
  20.     short            smokeWidth, smokeHeight;
  21.  
  22.     //Get cannon picture
  23.     cannon = NewBasicPICT(world, cannonID, cannonPlane, rCannonPic, 
  24.                                     transparent, cannonLeft, cannonTop);
  25.     if (cannon == nil) return false;
  26.     
  27.     //Get cannonball picture
  28.     thisElement = NewBasicPICT(world, ballID, ballPlane, rBallPic,
  29.                                     transparent, cannonLeft - 8, cannonTop - 8);
  30.     if (thisElement == nil) return false;
  31.  
  32.     //Initialize cannonball's motion
  33.     SetAutoChange(world, ballID, DoCannonBall, (Ptr) &ballMotion, 33);
  34.     
  35.     //Initialize motion parameters
  36.     ballMotion.limitRect = world->animationRect;
  37.     RectOffset(&ballMotion.limitRect, 0, -80);
  38.     InitMotion(&ballMotion, 2, 90);
  39.     
  40.     //Initialize cannonball's collision params
  41.     SetCollision(world, ballID, DoBallHit, 650); //Plane of pogo stick figure
  42.     
  43.     
  44.     //Cannonball is initially invisible
  45.     ShowElement(world, ballID, false);
  46.     
  47.     //Get animated smoke picture
  48.     thisElement = NewAnimatedGraphic(world, smokeID, smokePlane, rSmokePic,
  49.                                     transparent, 0, 0, 3);
  50.     if (thisElement == nil) return false;
  51.     
  52.     //Position smoke in relation to cannon
  53.     smokeWidth = thisElement->graphRect.right - thisElement->graphRect.left;
  54.     smokeHeight = thisElement->graphRect.bottom - thisElement->graphRect.top;
  55.     MoveElementTo(world, smokeID, cannonLeft - smokeWidth + 16, cannonTop - smokeHeight + 16);
  56.     
  57.     //Set smoke animation
  58.     AnimateGraphic(world, smokeID, 170, oneshot);
  59.     
  60.     //Smoke is initially invisible
  61.     ShowElement(world, smokeID, false);
  62.     
  63.     //Smoke is attached to cannon
  64.     cannon->slaveGrafEl = thisElement;
  65.     
  66.     //Get "FIRE" button
  67.     thisElement = NewButtonSensor(world, fBtnID, btnPlane, rFBtnPic, 434, 193);
  68.     if (thisElement == nil) return false;
  69.     SetSensorAction(world, fBtnID, ShootCannon);
  70.     
  71.     return true;
  72. }
  73.  
  74. pascal void DoCannonBall(GEWorldPtr world, GrafElPtr ball)
  75. {
  76.     MParamPtr    motion;
  77.     
  78.     motion = (MParamPtr) ball->changeData;
  79.     
  80.     if ((motion->currMotion.v == 0) && (motion->currMotion.h == 0)) {
  81.         ShowElement(world, ball->objectID, false);
  82.         return;
  83.     }
  84.     MoveElement(world, ball->objectID, motion->currMotion.h, motion->currMotion.v);
  85.     switch (CheckLimits(&ball->animationRect, &motion->limitRect)) {
  86.     case down:
  87.         if (motion->currMotion.v > 0) 
  88.             DoBounce(v, motion);
  89.         DoFriction(motion);
  90.         break;
  91.     case right:
  92.         if (motion->currMotion.h > 0) 
  93.             DoBounce(h, motion);
  94.         break;
  95.     case left:
  96.         if (motion->currMotion.h < 0) 
  97.             DoBounce(h, motion);
  98.         break;
  99.     default:
  100.         motion->currMotion.v++;
  101.         break;
  102.     }
  103. }
  104.  
  105. pascal void DoBallHit(GEWorldPtr world, GrafElPtr ball, GEDirection dir, GrafElPtr objHit)
  106. {
  107. #pragma unused (world, objHit)
  108.  
  109.     MParamPtr    motion;
  110.     
  111.     motion = (MParamPtr) ball->changeData;
  112.     switch (dir) {
  113.         case left:
  114.             if (motion->currMotion.h < 0)
  115.                 DoBounce(h, motion);
  116.             break;
  117.         case right:
  118.             if (motion->currMotion.h > 0)
  119.                 DoBounce(h, motion);
  120.             break;
  121.         case up:
  122.             if (motion->currMotion.v < 0)
  123.                 DoBounce(v, motion);
  124.             break;
  125.         case down:
  126.             if (motion->currMotion.v > 0)
  127.                 DoBounce(v, motion);
  128.             break;
  129.         case upLeft:
  130.             if (motion->currMotion.v < 0)
  131.                 DoBounce(v, motion);
  132.             if (motion->currMotion.h < 0)
  133.                 DoBounce(h, motion);
  134.             break;
  135.         case downLeft:
  136.             if (motion->currMotion.v > 0)
  137.                 DoBounce(v, motion);
  138.             if (motion->currMotion.h < 0)
  139.                 DoBounce(h, motion);
  140.             break;
  141.         case upRight:
  142.             if (motion->currMotion.v < 0)
  143.                 DoBounce(v, motion);
  144.             if (motion->currMotion.h > 0)
  145.                 DoBounce(h, motion);
  146.             break;
  147.         case downRight:
  148.             if (motion->currMotion.v > 0)
  149.                 DoBounce(v, motion);
  150.             if (motion->currMotion.h > 0)
  151.                 DoBounce(h, motion);
  152.             break;
  153.         
  154.     }
  155. }
  156.  
  157. pascal void ShootCannon(GEWorldPtr world, short fireIt)
  158. {
  159.     GrafElPtr cannon;
  160. #pragma unused (fireIt)
  161.  
  162.     cannon = FindElementByID(world, cannonID);
  163.     if (cannon) {
  164.         ballMotion.currMotion.h = -20;
  165.         ballMotion.currMotion.v = -20;
  166.         ballMotion.frictAcc = 0;
  167.         //Move ball into place at mouth of cannon
  168.         MoveElementTo(world, ballID, cannon->animationRect.left - 8, cannon->animationRect.top - 8);
  169.         ShowElement(world, ballID, true);
  170.         //Show smoke to start animation -- it will hide itself automatically
  171.         ShowElement(world, smokeID, true);
  172.     }
  173. }
  174.  
  175.